/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.xeerkat.agent.apps;

import java.io.File;
import java.io.FilenameFilter;
import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.representation.FileRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Finder;
import org.restlet.resource.ServerResource;
import org.xeerkat.agent.services.ServiceMetadata;

/**
 *
 * @author alex
 */
@ServiceMetadata(id="http://www.xeerkat.org/Services/Content/2008/1/0",version="1.0",vendor="xeerkat.org",copyright="2008 xeerkat.org")
public class ContentApplication extends Application {

   File dir;
   class FileResource extends ServerResource {
      File file;
      FileResource(File file) {
         this.file = file;
         setNegotiated(false);
      }
      public Representation get() {
         MediaType type = MediaType.APPLICATION_OCTET_STREAM;
         String name = file.getName();
         int pos = name.lastIndexOf('.');
         if (pos>0) {
            String ext = name.substring(pos+1);
            type = MediaType.valueOf(ContentApplication.this.getMetadataService().getMetadata(ext).getName());
         }
         return new FileRepresentation(file,type);
      }
   }
   class DirectoryResource extends ServerResource {
      File file;
      DirectoryResource(File file) {
         this.file = file;
         setNegotiated(false);
      }
      public Representation get() {
         final StringBuilder builder = new StringBuilder();
         builder.append("<html xmlns='http://www.w3.org/1999/xhtml'><head><title>");
         builder.append(file.getName());
         builder.append("</title></head><body><h1>");
         builder.append(file.getName());
         builder.append("</h1><ul>");
         file.list(new FilenameFilter() {
            public boolean accept(File parent,String name) {
               builder.append("<li><a href='"+name+"'>"+name+"</a></li>");
               return false;
            }
         }
         );
         builder.append("</ul></body></html>");
         return new StringRepresentation(builder.toString(),MediaType.TEXT_HTML);
      }
   }
   public ContentApplication(Context context) {
      super(context);
      String dirS = context.getParameters().getFirstValue("directory");
      if (dirS!=null) {
         dir = (new File(dirS)).getAbsoluteFile();
         if (!dir.exists()) {
            dir.mkdirs();
         }
         getLogger().info("Serving "+dir.getAbsolutePath());
      } else {
         getLogger().severe("The 'directory' parameter must be set on the "+ContentApplication.class.getName()+" application.");
      }
   }
   public Restlet createRoot() {
      if (dir==null) {
         getLogger().severe("No directory.");
         return null;
      } else {
         Finder finder = new Finder(getContext()) {
            public ServerResource create(Request request,Response response)
            {
               File f = new File(dir,request.getResourceRef().getRemainingPart());
               if (!f.exists()) {
                  return null;
               }
               if (f.isDirectory()) {
                  return new DirectoryResource(f);
               } else {
                  return new FileResource(f);
               }
            }
         };
         return finder;
      }
   }
}
